package com.avl;

import com.map.BSTMap;
import com.set.FileOperation;

import java.util.ArrayList;
import java.util.Collections;

public class PerformanceTest {

    public static void main(String[] args) {
        System.out.println("Test with book: pride-and-prejudice");
        String filePath = "E:\\WorkSpace\\LearningDemoWorkSpace\\data-structure\\src\\pride-and-prejudice.txt";
        ArrayList<String> words = new ArrayList<>();
        if (FileOperation.readFile(filePath, words)) {
            System.out.println("Total words: " + words.size());

            // 将数据排序成顺序添加入树，使得二分搜索树退化为链表（用来测试性能）
            Collections.sort(words);
            // BST tree
            long startTime = System.nanoTime();
            BSTMap<String, Integer> bst = new BSTMap<>();
            for (String word: words) {
                if (bst.contains(word)) {
                    bst.set(word, bst.get(word) + 1);
                } else {
                    bst.add(word, 1);
                }
            }
            long endTime = System.nanoTime();
            double time1 = (endTime - startTime)/1000000000.0;
            System.out.println("BST time: " + time1 + " s");

            // AVL tree
            long startTime02 = System.nanoTime();
            AVLTree<String, Integer> avlTree = new AVLTree<>();
            for (String word: words) {
                if (avlTree.contains(word)) {
                    avlTree.set(word, avlTree.get(word) + 1);
                } else {
                    avlTree.add(word, 1);
                }
            }
            long endTime02 = System.nanoTime();
            double time2= (endTime02 - startTime02)/1000000000.0;
            System.out.println("AVL time: " + time2 + " s");

        }
    }
}
